package modelo;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import com.thoughtworks.xstream.XStream;
import static modelo.SetUps.N_ARCH_OBJETOS;


public class PersistenciaObjetos {

	private Escenario escenario;
	
	public PersistenciaObjetos() {
	}

	public PersistenciaObjetos(Escenario escenario) {
		this.escenario = escenario;
	}

	public void persistir(String path) throws IOException {
		
		escenario.borrarAlgo42(); //Que hace esto aca?!
		ArrayList<Movil> objetos = escenario.getObjetos();
		//String pathObjetos  = new File (path,"1942-objetos.xml").toString();
		XStream xstream = new XStream();
		xstream.alias("Objetos", objetos.getClass());
		FileWriter xmlFile = new FileWriter(getPathObjetos(path));
		xstream.toXML(objetos,xmlFile);
		xmlFile.close();
		escenario.agregarObjeto(escenario.getJugador().getAlgo42());
	}

	public void cargar(String path, Escenario escenario) throws IOException {
		XStream xstreamObjetos = new XStream();
		ArrayList<Movil> objetos;
		xstreamObjetos.alias("Objetos", ArrayList.class);
		FileReader file = new FileReader(getPathObjetos(path));
		objetos = (ArrayList)xstreamObjetos.fromXML(file);
		file.close();
		for ( Movil m : objetos )
			escenario.agregarObjetoYVista(m);
	}
	
	private String getPathObjetos(String path){
		return new File (path,N_ARCH_OBJETOS).toString();
	}


}
